/* m_rsshortcut.c
 *   Contains the code for command shortcuts for ratbox-services
 *
 * Copyright (C) 2005 Lee Hardy <lee -at- leeh.co.uk>
 * Copyright (C) 2005 ircd-ratbox development team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1.Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * 2.Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 3.The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include "stdinc.h"
#include "tools.h"
#include "client.h"
#include "s_serv.h"
#include "modules.h"
#include "msg.h"
#include "send.h"
#include "hash.h"

static int m_ns(struct Client *, struct Client *, int, const char **);
static int m_cs(struct Client *, struct Client *, int, const char **);
static int m_os(struct Client *, struct Client *, int, const char **);
static int m_global(struct Client *, struct Client *, int, const char **);
static int m_watchserv(struct Client *, struct Client *, int, const char **);

struct Message ns_msgtab = {
	"NS", 0, 0, 0, MFLG_SLOW,
	{mg_ignore, {m_ns, 2}, mg_ignore, mg_ignore, mg_ignore, {m_ns, 2}}
};
struct Message cs_msgtab = {
	"CS", 0, 0, 0, MFLG_SLOW,
	{mg_ignore, {m_cs, 2}, mg_ignore, mg_ignore, mg_ignore, {m_cs, 2}}
};
struct Message os_msgtab = {
	"OS", 0, 0, 0, MFLG_SLOW,
	{mg_ignore, {m_os, 2}, mg_ignore, mg_ignore, mg_ignore, {m_os, 2}}
};
struct Message global_msgtab = {
	"GLOBAL", 0, 0, 0, MFLG_SLOW,
	{mg_ignore, {m_global, 2}, mg_ignore, mg_ignore, mg_ignore, {m_global, 2}}
};
struct Message watchserv_msgtab = {
	"WATCHSERV", 0, 0, 0, MFLG_SLOW,
	{mg_ignore, {m_watchserv, 2}, mg_ignore, mg_ignore, mg_ignore, {m_watchserv, 2}}
};
mapi_clist_av1 rsshortcut_clist[] = {
	&ns_msgtab,
	&cs_msgtab,
	&os_msgtab,
	&global_msgtab,
	&watchserv_msgtab,
	NULL
};

DECLARE_MODULE_AV1(rsshortcut, NULL, NULL, rsshortcut_clist, NULL, NULL, "1.0");


static int
m_ns(struct Client *client_p, struct Client *source_p,
	int parc, const char *parv[])
{
	char buf[BUFSIZE];
	int i = 1;

	if(find_server(NULL, "UniBG.services") == NULL)
	{
		sendto_one(source_p, 
			":%s 440 %s ns :Services are currently unavailable",
			me.name, source_p->name);
		return 0;
	}

	buf[0] = '\0';

	while(i < parc)
	{
		strlcat(buf, parv[i], sizeof(buf));
		strlcat(buf, " ", sizeof(buf));
		i++;
	}

	sendto_match_servs(client_p, "UniBG.services", CAP_ENCAP, NOCAPS,
			"ENCAP UniBG.services RSMSG ns %s",
			buf);
	return 0;
}

static int
m_cs(struct Client *client_p, struct Client *source_p,
	int parc, const char *parv[])
{
	char buf[BUFSIZE];
	int i = 1;

	if(find_server(NULL, "UniBG.services") == NULL)
	{
		sendto_one(source_p, 
			":%s 440 %s cs :Services are currently unavailable",
			me.name, source_p->name);
		return 0;
	}

	buf[0] = '\0';

	while(i < parc)
	{
		strlcat(buf, parv[i], sizeof(buf));
		strlcat(buf, " ", sizeof(buf));
		i++;
	}

	sendto_match_servs(client_p, "UniBG.services", CAP_ENCAP, NOCAPS,
			"ENCAP UniBG.services RSMSG cs %s",
			buf);
	return 0;
}

static int
m_os(struct Client *client_p, struct Client *source_p,
	int parc, const char *parv[])
{
	char buf[BUFSIZE];
	int i = 1;

	if(find_server(NULL, "UniBG.services") == NULL)
	{
		sendto_one(source_p, 
			":%s 440 %s os :Services are currently unavailable",
			me.name, source_p->name);
		return 0;
	}

	buf[0] = '\0';

	while(i < parc)
	{
		strlcat(buf, parv[i], sizeof(buf));
		strlcat(buf, " ", sizeof(buf));
		i++;
	}

	sendto_match_servs(client_p, "UniBG.services", CAP_ENCAP, NOCAPS,
			"ENCAP UniBG.services RSMSG os %s",
			buf);
	return 0;
}

static int
m_global(struct Client *client_p, struct Client *source_p,
	int parc, const char *parv[])
{
	char buf[BUFSIZE];
	int i = 1;

	if(find_server(NULL, "UniBG.services") == NULL)
	{
		sendto_one(source_p, 
			":%s 440 %s global :Services are currently unavailable",
			me.name, source_p->name);
		return 0;
	}

	buf[0] = '\0';

	while(i < parc)
	{
		strlcat(buf, parv[i], sizeof(buf));
		strlcat(buf, " ", sizeof(buf));
		i++;
	}

	sendto_match_servs(client_p, "UniBG.services", CAP_ENCAP, NOCAPS,
			"ENCAP UniBG.services RSMSG global %s",
			buf);
	return 0;
}

static int
m_watchserv(struct Client *client_p, struct Client *source_p,
	int parc, const char *parv[])
{
	char buf[BUFSIZE];
	int i = 1;

	if(find_server(NULL, "UniBG.services") == NULL)
	{
		sendto_one(source_p, 
			":%s 440 %s watchserv :Services are currently unavailable",
			me.name, source_p->name);
		return 0;
	}

	buf[0] = '\0';

	while(i < parc)
	{
		strlcat(buf, parv[i], sizeof(buf));
		strlcat(buf, " ", sizeof(buf));
		i++;
	}

	sendto_match_servs(client_p, "UniBG.services", CAP_ENCAP, NOCAPS,
			"ENCAP UniBG.services RSMSG watchserv %s",
			buf);
	return 0;
}
